package com.clover.framework.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;

/*
 * JSON 格式帮助类
 * @Author masl - 2019/4/13 8:28
 */
public class JsonUtils {
    public static String toString(Object data) {
        return JSON.toJSONString(data, SerializerFeature.WriteDateUseDateFormat);
    }

    public static <T> T convertObject(Object data, Class<T> clazz) {
        return JSON.parseObject(toString(data), clazz);
    }

    /**
     * List<T> 转 json 保存到数据库
     */
    public static <T> String listToJson(List<T> ts) {
        String jsons = JSON.toJSONString(ts);
        return jsons;
    }

    /**
     * json 转 List<T>
     */
    public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }
}
